package net.bplaced.streetfighter.utils.update;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Date;
import java.util.UUID;

import android.util.Log;

/**
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.com>
 */
public class ExceptionHandler
        implements UncaughtExceptionHandler {
    private final UncaughtExceptionHandler defaultExceptionHandler;

    public ExceptionHandler(UncaughtExceptionHandler defaultExceptionHandler) {

        this.defaultExceptionHandler = defaultExceptionHandler;
    }

    @Override
    public void uncaughtException(Thread thread, Throwable exception) {

        final Date now = new Date();
        final Writer result = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(result);

        exception.printStackTrace(printWriter);

        try {
            // Create filename from a random uuid
            final String filename = UUID.randomUUID().toString();
            final String path = Constants.FILES_PATH + "/" + filename + ".stacktrace";
            Log.d(Constants.TAG, "Writing unhandled exception to: " + path);

            // Write the stacktrace to disk
            final BufferedWriter write = new BufferedWriter(new FileWriter(path));
            write.write("Package: " + Constants.APP_PACKAGE + "\n");
            write.write("Version: " + Constants.APP_VERSION + "\n");
            write.write("Android: " + Constants.ANDROID_VERSION + "\n");
            write.write("Manufacturer: " + Constants.PHONE_MANUFACTURER + "\n");
            write.write("Model: " + Constants.PHONE_MODEL + "\n");
            write.write("Date: " + now + "\n");
            write.write("\n");
            write.write(result.toString());
            write.flush();
            write.close();
        } catch (final Exception another) {
            Log.e(Constants.TAG, "Error saving exception stacktrace!\n", another);
        }

        this.defaultExceptionHandler.uncaughtException(thread, exception);
    }
}